<template>
  <div>
    <van-nav-bar
      fixed
      placeholder
      safe-area-inset-top
      title="我的房源管理"
      @click-left="back"
    >
      <template #left>
        <van-icon name="arrow-left" size="16px" color="#333" />
      </template>
      <template #right>
        <van-icon @click="toadd" size="20px" color="#45B7CB" name="add-o" />
      </template>
    </van-nav-bar>
    <div class="nomore" v-if="houselist.length == 0">
      您还没有上传房源，点击<van-icon
        size="20px"
        color="#45B7CB"
        name="add-o"
        style="vertical-align: middle"
      />发布
    </div>
    <div v-for="item in houselist" :key="item.index" class="hpart">
      <div class="house">
        <van-image
          width="100px"
          height="100px"
          lazy-load
          :src="item.houseImage"
          fit="cover"
          radius="5px"
        />
        <div class="intrduce">
          <span class="title1">{{ item.subdistrict }}</span>
          <span class="title2"
            ><span>{{ item.houseType }}</span
            >|<span>{{ item.coveredArea }}</span
            >|<span>{{ item.floor }}</span></span
          >
          <span v-show="item.distance" class="title2"
            ><van-icon name="location" />{{ item.distance }}</span
          >
          <span class="title3" style="font-size: 15px">{{
            item.listedPrice
          }}</span>
          <span class="title3" v-if="item.trust_finish == 1"
            >委托录入已完成</span
          >
        </div>
      </div>
      <div>
        <div v-show="item.houseCheckStatus == 1" class="btnpart">
          <van-button class="inbox" style="margin-left: 10px"
            >等待核验</van-button
          >
          <van-button
            class="inbox"
            style="margin-left: 10px"
            @click="goHouseInfo(item.houseId)"
            >查看房源</van-button
          >
          <!-- <van-button
            class="inbox"
            @click="changehouse(item.houseId, item.is_agent)"
            v-show="item.houseCheckStatus == 1 && item.is_agent == 1"
            >修改</van-button
          > -->
        </div>
        <div v-show="item.houseCheckStatus == 3" class="btnpart">
          <van-button class="inbox">核验未通过</van-button>
          <van-button
            @click="changehouse(item.houseId, item.is_agent)"
            class="borbox"
            >重新编辑</van-button
          >
        </div>
        <div
          v-show="item.houseCheckStatus == 2 && item.is_agent == 0"
          class="btnpart"
        >
          <van-button
            class="inbox"
            @click="changehouse(item.houseId, item.is_agent)"
            >修改</van-button
          >
          <van-button
            v-show="item.putaway == 2"
            @click="showdownhouse(item.houseId)"
            class="borbox"
            >下架</van-button
          >
          <van-button
            v-show="item.putaway == 1"
            @click="uphouse(item.houseId)"
            class="borbox"
            >上架</van-button
          >
        </div>
      </div>
    </div>
    <van-popup v-model="show">
      <div class="content">
        <div class="box1">下架房源</div>
        <div class="box2">下架后将不再享受房源推荐曝光，你确认下架房源吗？</div>
        <div class="btn">
          <button @click="downhouse()">确认下架</button>
          <button @click="show = false">我再想想</button>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import { getAgenthouse, buyhouseupdown } from "../../apis/house.js";
export default {
  data() {
    return {
      houselist: [],
      show: false,
      downid: "",
    };
  },
  created() {
    this.getlist();
  },
  methods: {
    goHouseInfo(id) {
      this.$router.push({
        path: "/AgentHouseInfo",
        query: {
          houseId: id,
        },
      });
    },
    back() {
      this.$router.go(-1);
    },
    uphouse(id) {
      buyhouseupdown({ put: "2", houseId: id }).then((res) => {
        if (res.code == 200) {
          this.getlist();
          this.$toast("上架成功");
        } else if (res.code == 2072) {
          this.$toast(res.msg);
        }
      });
    },
    showdownhouse(id) {
      this.show = true;
      this.downid = id;
    },
    downhouse() {
      buyhouseupdown({ put: "1", houseId: this.downid }).then((res) => {
        if (res.code == 200) {
          this.getlist();
          this.show = false;
          this.$toast("下架成功");
        }
      });
    },
    getlist() {
      getAgenthouse().then((res) => {
        console.log(res);
        this.houselist = res.result.list;
      });
    },
    toadd() {
      this.$router.push({ path: "/releaseagent" });
    },
    changehouse(id, isAgent) {
      this.$router.push({
        path: "/releaseagent",
        query: { houseId: id, isAgent: isAgent },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.nomore {
  color: gray;
  text-align: center;
  margin: 20px 0;
}
.hpart {
  margin: 0 15px;
  overflow: hidden;
  padding: 10px;
  border: 1px solid #ccc;
  box-shadow: 0px 1px 10px 5px #eee;
  margin-bottom: 10px;
  border-radius: 10px;
  .house {
    display: flex;
    margin: 10px 0;
    img {
      width: 100px;
      height: 100px;
    }
    .intrduce {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      margin-left: 10px;
      .title1 {
        font-size: 16px;
        font-weight: 550;
        color: #333333;
      }
      .title2 {
        font-size: 12px;
        color: #a0a0a0;
        span {
          margin: 0 2px;
        }
        .up {
          font-size: 9px;
          vertical-align: super;
        }
      }
      .title3 {
        color: #d43b33;
        font-size: 16px;
        font-weight: bold;
        span {
          font-size: 12px;
          margin-left: 3px;
        }
      }
    }
  }
  .btnpart {
    display: flex;
    flex-direction: row-reverse;
    .van-button {
      height: 35px;
      font-size: 12px;
      border-radius: 20px;
      border: none;
      padding: 0 15px;
    }
    .borbox {
      color: #45b7cb;
      border: 1px solid #45b7cb;
      margin-right: 10px;
    }
    .inbox {
      background-color: #45b7cb;
      color: #fff;
      margin-right: 0px;
    }
  }
  .roomhouse {
    display: flex;
    margin: 10px 0;
    img {
      width: 100px;
      height: 100px;
    }
    .intrduce {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      margin-left: 10px;
      .title1 {
        font-size: 16px;
        font-weight: 550;
        color: #333333;
      }
      .title2 {
        font-size: 12px;
        color: #a0a0a0;
        span {
          margin: 0 2px;
        }
        .up {
          font-size: 9px;
          vertical-align: super;
        }
      }
      .title3 {
        color: #d43b33;
        font-size: 16px;
        font-weight: bold;
        span {
          font-size: 12px;
          margin-left: 3px;
        }
      }
    }
  }
}
.content {
  width: 300px;
  height: 180px;
  background-color: #fff;
  text-align: center;
  .box1 {
    padding: 20px 0;
    font-size: 16px;
    color: #45b7cb;
    font-weight: bold;
  }
  .box2 {
    margin: 0 auto;
    width: 210px;
    font-size: 14px;
    color: #333;
  }
  .btn {
    margin-top: 30px;
    button {
      font-size: 14px;
      margin: 0 10px;
      width: 110px;
      height: 35px;
      border-radius: 20px;
      &:first-child {
        background-color: #fff;
        border: 1px solid #f1f1f1;
        color: #999;
      }
      &:last-child {
        background-color: #45b7cb;
        border: 1px solid #45b7cb;
        color: #fff;
      }
    }
  }
}
</style>